import Request from '@/utils/request'
import Cookies from '@/utils/auth'

const state = () => ({
	adminAddress: false,  // 管理地址
	detail: {},
	addressList:[], // 收货地址列表
	curAddress:{}, // 当前选择的地址
	orderInfo:{ // 购买信息
		goodsId:'',
		quantity:1,  // 购买数量
		specs:{specGroupName:'',specName:''}, // 购买的规格:g规格分组名称、规格名称
		params:{paramName:'',paramValue:''}, // 购买的参数：参数名称，参数值
		chainType:'', // 链类型,ETH,CXT
		currency:'',// 币类型:可用值:BCXT,CNY,CXT,ETH,USD,USDT
		logisticsType:'', // 物流类型，1:物流送货，2:自提
		selfAddress:'', // 自提点地址及联系方式：‘地址|联系方式’
	},
	freightPrice:0 // 物流费用
})
const getters = {
	
}
const mutations = {
	set_adminAddress: (state, data) => {
		state.adminAddress = data
	},
	set_detail: (state, data) => {
		state.detail = data
	},
	set_addressList: (state, data) => {
		state.addressList = data
	},
	set_curAddress: (state, data) => {
		state.curAddress = data
	},
	set_orderInfo: (state, obj) => {
		Object.assign(state.orderInfo,obj)  // 把obj合并到orderInfo中，如有相同属性的则替换值
	},
	set_freightPrice: (state, data) => {
		state.freightPrice = data
	},
}
const actions = {
	async get_shop_list({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/shop/list',params,{unLoading:true})
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async get_shop_detail({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/shop/detail',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async get_goods_list({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/goods/list',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async get_coupon_list({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/shop/couponListAddressShop',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async get_coupon_receive({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/shop/couponListShop',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},

	async couponListAddressShop({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/shop/couponListShop',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async couponReceive({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/shop/couponReceive',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},

	async couponCancel({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/shop/couponCancel',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},


	async get_goods_detail({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/goods/detail',params,{unLoading:true})
			.then(res=>{
				if(res.data.success){
					commit('set_detail',res.data.data)
				}
				resolve(res.data) 
			})
		})
	},
	async get_goods_type_list({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/goods/category/all',params,{unLoading:true})
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async get_area_list({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/area/list',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async add_address({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/userShippingAddress/add',params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async del_address({commit}, userShippingAddressId) { 
		return new Promise((resolve,reject)=>{
			Request.post(`/api/userShippingAddress/delete/${userShippingAddressId}`)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async couponListUser({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post(`/api/shop/couponListUser`,params)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},

	async get_address_list({commit}, params) { 
		return new Promise((resolve,reject)=>{
			Request.post('/api/userShippingAddress/list',params)
			.then(res=>{
				if(res.data.success){
					commit('set_addressList',res.data.data)
				}
				resolve(res.data) 
			})
		})
	},

	
	async create_number_order({commit,state},params) { 
		return new Promise((resolve,reject)=>{
			let baseParams ={
				shopCouponId:params.shopCouponId,
				chainType:state.orderInfo.chainType,
				currency:state.orderInfo.currency,
				goodsAttr:state.orderInfo.specs.specGroupName+"|"+state.orderInfo.specs.specName,
				goodsId:state.orderInfo.goodsId,
				quantity:state.orderInfo.quantity,
				logisticsType:params.logisticsType
			}
			if(params.logisticsType==2){ // 自提
				let selfParams ={
					shopCouponId:params.shopCouponId,
					logisticsSelfAddress:'1',
					logisticsSelfPhone:'1'
				}
				Object.assign(baseParams,selfParams)
			}else{ // 物流配送
				let deliveryParams ={
					shopCouponId:params.shopCouponId,
					logisticsDeliveryCityId:state.curAddress.cityId,	//物流送货城市id
					logisticsDeliveryCityName:state.curAddress.cityName,	//物流送货城市名称	
					logisticsDeliveryCountryId:state.curAddress.countryId,	//物流送货国家ID
					logisticsDeliveryCountryName:state.curAddress.countryName,	//物流送货国家名称
					shippingAddress:state.curAddress.address,// 收货地址
					shippingName:state.curAddress.name, // 收货人姓名
					shippingPhone:state.curAddress.phone // 收货人联系电话
				}
				Object.assign(baseParams,deliveryParams)
			}
			Object.assign(baseParams,params)
			Request.post('/api/mall/order/create',baseParams)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},

	async create_order({commit,state},params) { 
		return new Promise((resolve,reject)=>{
			let baseParams ={
				shopCouponId:params.shopCouponId,
				chainType:state.orderInfo.chainType,
				currency:state.orderInfo.currency,
				goodsAttr:state.orderInfo.specs.specGroupName+"|"+state.orderInfo.specs.specName,
				goodsId:state.orderInfo.goodsId,
				quantity:state.orderInfo.quantity,
				logisticsType:state.orderInfo.logisticsType
			}
			if(state.orderInfo.logisticsType==2){ // 自提
				let selfParams ={
					shopCouponId:params.shopCouponId,
					logisticsSelfAddress:state.orderInfo.selfAddress.split("|")[0],
					logisticsSelfPhone:state.orderInfo.selfAddress.split("|")[1]
				}
				Object.assign(baseParams,selfParams)
			}else{ // 物流配送
				let deliveryParams ={
					shopCouponId:params.shopCouponId,
					logisticsDeliveryCityId:state.curAddress.cityId,	//物流送货城市id
					logisticsDeliveryCityName:state.curAddress.cityName,	//物流送货城市名称	
					logisticsDeliveryCountryId:state.curAddress.countryId,	//物流送货国家ID
					logisticsDeliveryCountryName:state.curAddress.countryName,	//物流送货国家名称
					shippingAddress:state.curAddress.address,// 收货地址
					shippingName:state.curAddress.name, // 收货人姓名
					shippingPhone:state.curAddress.phone // 收货人联系电话
				}
				Object.assign(baseParams,deliveryParams)
			}
			Object.assign(baseParams,params)
			Request.post('/api/mall/order/create',baseParams)
			.then(res=>{
				resolve(res.data) 
			})
		})
	},
	async get_freight_price({commit,state},params) {
		if(state.orderInfo.logisticsType==2){ // 自提
			commit('set_freightPrice',0)
		}else if(state.detail.apiShopDetailDto && state.detail.apiShopDetailDto.deliveryExpenses==1){ // 免费包邮
			commit('set_freightPrice',0)
		}else{
			if(state.curAddress.cityId){ // 获取当前选择地址城市的物流费用
				if(state.detail.apiShopDetailDto && state.detail.apiShopDetailDto.deliveryPlaces.length>0){
					for(let i=0;i<state.detail.apiShopDetailDto.deliveryPlaces.length;i++){
						const item = state.detail.apiShopDetailDto.deliveryPlaces[i]
						if(item.countryId==state.curAddress.countryId && item.cityId==state.curAddress.cityId){
							commit('set_freightPrice',item.logisticsAmount || 0)
							break
						}
					}
				}else{
					commit('set_freightPrice',0)
				}
			}else{
				commit('set_freightPrice',0)
			}
		}
	}
}

export default {
	namespaced: true,
	state,
	getters,
	mutations,
	actions
}
